package com.ttc.group2.videochat.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.quickblox.users.model.QBUser;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.activity.CreateGroupActivity;
import com.ttc.group2.videochat.activity.DialCallActivity;
import com.ttc.group2.videochat.adapter.PeopleListAdapter;
import com.ttc.group2.videochat.model.DataHolder;
import com.ttc.group2.videochat.model.Section;

public class PeopleFragment extends ListFragment  {
	
	private static final String TAG = "PeopleFragment";
	private List<QBUser> peoples;
	private PeopleListAdapter mAdapter = new PeopleListAdapter();
	private HashMap<String, Integer> sections = new HashMap<String, Integer>();

	private PeopleFragment() {
	};

	public static PeopleFragment newIntance() {
		PeopleFragment peoleFragment = new PeopleFragment();
		return peoleFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.fragment_people, container, false);
		
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// get from server
		peoples = DataHolder.getDataHolder().getQbUsers();

		List<QBUser> rows = new ArrayList<QBUser>();
		int start = 0;
		String previousLetter = null;
		Pattern numberPattern = Pattern.compile("[0-9]");

		for (QBUser qbUser : peoples) {
			String firstLetter = qbUser.getFullName().substring(0, 1);

			// Group numbers together
			if (numberPattern.matcher(firstLetter).matches()) {
				firstLetter = "#";
			}

			if (!firstLetter.equalsIgnoreCase(previousLetter)) {
				rows.add(new Section(firstLetter));
				sections.put(firstLetter, start);
			}
			QBUser user = new QBUser();
			user.setFullName(qbUser.getFullName());
			user.setEmail(qbUser.getEmail());
			rows.add(user);
			previousLetter = firstLetter;
		}
		mAdapter.setRows(rows);
		setListAdapter(mAdapter);

		// Set empty view
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
//		String item=(String)getListAdapter().getItem(position);
		super.onListItemClick(l, v, position, id);
//		Toast.makeText(this, item+" ", Toast.LENGTH_LONG);
//		  Toast.makeText(getActivity().getApplicationContext(),
//			      "Click ListItem Number " + position, Toast.LENGTH_LONG)
//			      .show();
//		Intent i=new Intent(v.getContext(), DialCallActivity.class);
//		startActivity(i);
		
//		Intent intent = new Intent(getActivity().getApplicationContext(), CreateGroupActivity.class);
//		startActivity(intent);

		 v.findViewById(R.id.btn_call).setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	               Intent intent = new Intent(v.getContext(), CreateGroupActivity.class);
//	               startActivity(intent);
	            }
	        });
	}
}
